import { buildApiUrl, reqDelete, reqGet, reqPost, reqPut } from '@render/util/AjaxUtil'
import FrontPageResult from '@render/util/FrontPageResult'
import { cloneDeep } from 'lodash-unified'

export async function add(bo: ApiInfo) {
  const url = buildApiUrl('/api/add')
  await reqPost(url, bo)
}

export async function copyAdd(id: number) {
  const url = buildApiUrl(`/api/copy-add/${id}`)
  await reqPost(url)
}

export async function modify(bo: ApiInfo) {
  const url = buildApiUrl('/api/modify')
  const ret = cloneDeep(bo)
  delete ret['createdAt']
  delete ret['updatedAt']
  delete ret['_X_ROW_KEY']
  await reqPut(url, ret)
}

export async function page(projectId: number, current: number, size: number, kw: string) {
  const url = buildApiUrl('/api/page')
  return await reqGet<FrontPageResult<ApiInfo>>(url, { projectId, current, size, kw })
}

export async function remove(ids: number[]) {
  const url = buildApiUrl('/api/remove')
  await reqDelete(url, { id: ids })
}

export async function saveMockMethodContent(apiId: number, methodContent: string) {
  const url = buildApiUrl('/api/save-mock-method-conetent')
  await reqDelete(url, { id: apiId, mockMethodContent: methodContent })
}
